#include "configfile.h"





configInfo::configInfo(int tomatoTime, int tomatoTime_rest, int tomatoAchieveToday, QString finishedTask, QString date, QString bellCurrent, int bellLoop, bool closeAndMinimize,
                       bool autoRun,QString defaultDescribe, QString serverAddress):
    m_tomatoTime(tomatoTime)
  ,m_tomatoTime_rest(tomatoTime_rest)
  ,m_tomatoAchieveToday(tomatoAchieveToday)
  ,m_finishedTask(finishedTask)
  ,m_bellCurrent(bellCurrent)
  ,m_bellLoop(bellLoop)
  ,m_closeAndMinimize(closeAndMinimize)
  ,m_defaultDescribe(defaultDescribe)
  ,m_autoRun(autoRun)
  ,m_serverAddress(serverAddress)
{
    date.isEmpty()?m_date=QDateTime::currentDateTime().toString("yyyy-MM-dd"):m_date=date;
    if (m_defaultDescribe.isEmpty())
        m_defaultDescribe = "与事物建立起深度关系,"
                          "而能力,是你的存在与事物的存在相遇时的副产物。";
};


configFile::configFile()
{
    QStringList pathList;
    pathList << QApplication::applicationDirPath() << "configFile.ini";
    m_qstrFileName = pathList.join(QDir::separator());
    InitConfig();
}

void configFile::InitConfig()
{

    qDebug() << m_qstrFileName;

    QDir dir;

    if ( !dir.exists(m_qstrFileName) ){
        QFile temp_file(m_qstrFileName);
        temp_file.open(QIODevice::WriteOnly);
        temp_file.close();
        qDebug() <<"新建 "<<m_qstrFileName<<" 配置文件 " << m_qstrFileName;
        m_psetting = new QSettings(m_qstrFileName, QSettings::IniFormat);
        save();
    } else {
        qDebug() <<"读取配置文件 " << m_qstrFileName;
        m_psetting = new QSettings(m_qstrFileName, QSettings::IniFormat);
        m_config.m_tomatoTime = this->Get("Application","tomatoTime").toInt();
        m_config.m_tomatoTime_rest = this->Get("Application","tomatoTime_rest").toInt();
        m_config.m_finishedTask = this->Get("Application","finishedTask").toString();
        m_config.m_bellCurrent = this->Get("Application","bellCurrent").toString();
        m_config.m_bellLoop = this->Get("Application","bellLoop").toInt();
        m_config.m_closeAndMinimize = this->Get("Application","closeAndMinimize").toBool();
        m_config.m_autoRun = this->Get("Application","autoRun").toBool();
        m_config.m_defaultDescribe = this->Get("Application","defaultDescribe").toString();
        m_config.m_serverAddress = this->Get("Application","serverAddress").toString();
        if (m_config.m_date == this->Get("Application", "date").toString())
            m_config.m_tomatoAchieveToday = this->Get("Application","tomatoAchieveToday").toInt();
        else
            m_config.m_tomatoAchieveToday = 0;
    }

}

configFile::~configFile()
{
    delete m_psetting;
    m_psetting = 0;
}

void configFile::Set(QString qstrNodeName,QString qstrKeyName,QVariant qvarvalue)
{
    m_psetting->setValue(QString("/%1/%2").arg(qstrNodeName).arg(qstrKeyName), qvarvalue);

}

QVariant configFile::Get(QString qstrNodeName,QString qstrKeyName)
{
    QVariant qvar = m_psetting->value(QString("/%1/%2").arg(qstrNodeName).arg(qstrKeyName));
    return qvar;
}

void configFile::save()
{
    this->Set("Application","tomatoTime", QVariant(m_config.m_tomatoTime));
    this->Set("Application","tomatoTime_rest", QVariant(m_config.m_tomatoTime_rest));
    this->Set("Application","finishedTask", QVariant(m_config.m_finishedTask));
    this->Set("Application","tomatoAchieveToday", QVariant(m_config.m_tomatoAchieveToday));
    this->Set("Application","date", QVariant(m_config.m_date));
    this->Set("Application","bellCurrent", QVariant(m_config.m_bellCurrent));
    this->Set("Application","bellLoop", QVariant(m_config.m_bellLoop));
    this->Set("Application","closeAndMinimize", QVariant(m_config.m_closeAndMinimize));
    this->Set("Application","autoRun", QVariant(m_config.m_autoRun));
    this->Set("Application","defaultDescribe", QVariant(m_config.m_defaultDescribe));
    this->Set("Application","serverAddress", QVariant(m_config.m_serverAddress));
}
